//
//  Created by matt on 28/09/12.
//

#import <sys/ucred.h>
#import "TipItemView.h"
#import "UIImageView+WebCache.h"
#import "TipTextItem.h"
#import "TipImageItem.h"

#define kPlaceholderImageNameDefault @"default_01"

@interface TipItemView()

@property (nonatomic, strong) UITextView *textView;

@end

@implementation TipItemView

#pragma mark - Init

- (void)setup 
{

  // positioning
  self.topMargin = 8;
  self.leftMargin = 8;

  // background
  self.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.95 alpha:1];

  // shadow
  self.layer.shadowColor = [UIColor colorWithWhite:0.12 alpha:1].CGColor;
  self.layer.shadowOffset = CGSizeMake(0, 0.5);
  self.layer.shadowRadius = 1;
  self.layer.shadowOpacity = 1;
}

#pragma mark - Factories


+ (TipItemView *)itemAddBoxWithSize:(CGSize)size
{

  // basic box
  TipItemView *box = [TipItemView boxWithSize:size];
    
  // style and tag
  box.tag = -1;

  // add the add image
  UIImage *add = [UIImage imageNamed:@"add_tip_item_btn"];
  UIImageView *addView = [[UIImageView alloc] initWithImage:add];
  [box addSubview:addView];
  addView.center = (CGPoint){box.width / 2, box.height / 2};

addView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin
      | UIViewAutoresizingFlexibleRightMargin
      | UIViewAutoresizingFlexibleBottomMargin
      | UIViewAutoresizingFlexibleLeftMargin;
	box.layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tip_item_background"]].CGColor;
  return box;
}


+ (TipItemView *)tipItemViewWithTipItem:(TipItem *)item withSize:(CGSize)size
{
    // box with photo number tag
    TipItemView *box = [TipItemView boxWithSize:size];
    box.tipItem = item;
    

    // do the photo loading async, because internets
    __weak typeof(box) weakBox = box;
    box.asyncLayoutOnce = ^{
        [weakBox loadItemData];
    };
	
	return box;

}



#pragma mark - Layout

- (void)layout
{
  [super layout];

  // speed up shadows
  self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
}

- (UITextView *)textView
{
    if (!_textView)
    {
        _textView =  [[UITextView alloc] init];
    }
    return _textView;
}

- (void)setText:(NSString *)text
{
    TipTextItem *textItem = (TipTextItem *) self.tipItem;
    textItem.textContent = text;
    self.textView.text = text;


}

- (void)layoutSubviews
{
	[super layoutSubviews];
	
	[self.textView sizeToFit];

	self.textView.centerX = (CGFloat) (self.width/2.0);
	self.textView.centerY = (CGFloat) (self.height/2.0);
}

#pragma mark - Photo box loading

- (void)loadItemData
{

  // do UI stuff back in UI land
  dispatch_async(dispatch_get_main_queue(), ^{


      if ([self.tipItem isKindOfClass:[TipTextItem class]])
      {

          TipTextItem *textItem = (TipTextItem *) self.tipItem;
          // got the photo, so lets show it
          UITextView *label = self.textView;
          [self addSubview:label];
          label.size = self.size;
          label.alpha = 0;
		  label.backgroundColor = [UIColor clearColor];
		  label.textAlignment = NSTextAlignmentCenter;
		  label.editable = NO;
          label.text = textItem.textContent;
          label.autoresizingMask = UIViewAutoresizingFlexibleWidth
                                       | UIViewAutoresizingFlexibleHeight;

          // fade the image in
          [UIView animateWithDuration:0.2 animations:^{
              label.alpha = 1;
          }];
      }
      else if ([self.tipItem isKindOfClass:[TipImageItem class]])
      {


          TipImageItem *imageItem = (TipImageItem *) self.tipItem;
          // got the photo, so lets show it
          UIImageView *imageView = [[UIImageView alloc] init];
          [self addSubview:imageView];
          imageView.size = self.size;

          imageView.alpha = 0;
          imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth
                                       | UIViewAutoresizingFlexibleHeight;

		  imageView.contentMode = UIViewContentModeScaleAspectFit;
          if (imageItem.image)
          {
              [imageView setImage:imageItem.image];
          }
          else
          {
			  if (self.needClips)
			  {
				  [imageView setQNImageWithURL:[NSURL URLWithString:imageItem.imageUrl]
							   placeholderImage:[UIImage imageNamed:kPlaceholderImageNameDefault]];
			  }
			  else
			  {
				  [imageView setQNImageAdjustHeightWithURL:[NSURL URLWithString:imageItem.imageUrl]
							   placeholderImage:[UIImage imageNamed:kPlaceholderImageNameDefault]];
			  }

          }
      
          // fade the image in
          [UIView animateWithDuration:0.2 animations:^{
              imageView.alpha = 1;
          }];
          
      }

  });
}



@end
